﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class FinanceCenter_EarningListForPrint : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            string StartTime = Request["StartTime"];
            string EndTime = Request["EndTime"];
            tbStartTime.Value = ShoveEL._Convert.StrToDateTime(StartTime, DateTime.Now.ToShortDateString()).ToString();
            tbEndTime.Value = ShoveEL._Convert.StrToDateTime(EndTime, DateTime.Now.ToShortDateString()).ToString();

            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestCompetences = Competences.BuildCompetencesList(Competences.Finance);
        RequestLoginPage = "FinanceCenter/EarningListForPrint.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        DateTime StartTime = ShoveEL._Convert.StrToDateTime(tbStartTime.Value.ToString(), DateTime.Now.ToShortDateString());
        DateTime EndTime = ShoveEL._Convert.StrToDateTime(tbEndTime.Value.ToString(), DateTime.Now.ToShortDateString());

        int Result = 0;
        string Return = "";

        DataSet ds = DAL.Procedures.P_InComingOrOutComing.Open(StartTime, EndTime, 1, ref Result, ref Return);

        if (ds == null)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "数据库连接失败。");

            return;
        }

        g.DataSource = ds.Tables[0].DefaultView;
        g.DataBind();

        if (ds.Tables[1].Rows.Count > 0)
        {
            string strMoney = ds.Tables[1].Rows[0]["SumMoney"].ToString();
            lbUserCount.Text = ds.Tables[1].Rows[0]["SumID"].ToString();

            lbBuyCount.Text = (ShoveEL._Convert.StrToDouble(strMoney, 0) == 0) ? "0.00" : ShoveEL._Convert.StrToDouble(strMoney, 0).ToString("N");
        }
    }

    protected void g_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            for (int i = 4; i <= 4; i++)
            {
                double money = ShoveEL._Convert.StrToDouble(e.Item.Cells[i].Text, 0);
                e.Item.Cells[i].Text = (money == 0) ? "" : money.ToString("N");
            }

            for (int i = 1; i <= ShoveEL._Convert.StrToInt(e.Item.Cells[7].Text, 1); i++)
            {
                e.Item.Cells[3].Text += "<img src='../Images/gold.gif' border='0'>";
            }


            string sequenceNumber = e.Item.Cells[0].Text.ToString();
            if (ShoveEL._Convert.StrToInt(sequenceNumber, 0) >= 10)
            {
                e.Item.Cells[0].Text = "0" + sequenceNumber;
            }
            else
            {
                e.Item.Cells[0].Text = "00" + sequenceNumber;
            }
        }
    }

    protected void btnDownload_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=EarningList.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        g.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
}